package co.id.btel;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import utilities.Constants;
import utilities.FileConfig;
import utilities.PdfUtil;
import co.id.btel.create.pdf.PdfCustomer;
import co.id.btel.create.pdf.PdfFooter;
import co.id.btel.create.pdf.PdfHeader;
import co.id.btel.create.pdf.PdfSummary;
import co.id.btel.create.pdf.PdfTotal;
import co.id.btel.create.pdf.PdfUsage;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfWriter;

public class AHAMainService {

	private static final Logger logger = Logger.getLogger(AHAMainService.class);
	private static String MODE = FileConfig.getValue(Constants.ALL,
			Constants.MODE_EXECUTION);
	private static String FILE_PDF = FileConfig.getValue(MODE, Constants.PATH);
	private static String IMAGE = FileConfig.getValue(MODE, Constants.IMG);

	// private static final long started = System.currentTimeMillis();
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		try {
			PropertyConfigurator.configure(System.getProperty("log4j"));
			logger.info("Program Started...");
			// cek file
			File file = new File(FILE_PDF);
			if (file.exists())
				file.delete();
			// start create
			Document document = new Document();
			PdfWriter.getInstance(document, new FileOutputStream(FILE_PDF));

			Image img = Image.getInstance(IMAGE);
			img.scaleToFit(PageSize.LEGAL.getHeight() * 1.38f,
					PageSize.LEGAL.getWidth() * 1.38f);
			img.setAbsolutePosition(0, 0);

			document.open();
			document.add(img);
			PdfUtil.addMetaData(document);
			PdfHeader.addHeader(document);
			PdfCustomer.addCustomer(document);
			PdfSummary.addSummary(document);
			PdfUsage.addInternetUsage(document);
			PdfUsage.addVoiceUsage(document);
			PdfUsage.addVASUsage(document);
			PdfUsage.addOtherUsage(document);
			PdfTotal pt = new PdfTotal();
			pt.addTotal(document);
			PdfFooter.addFooter(document);
			document.close();
			logger.info("Program Finished");
		} catch (DocumentException ex) {
			ex.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
